﻿using DevExpress.XtraEditors;
using DevExpress.XtraRichEdit;
using DLL;
using NursingFormSystem;
using NursingFormSystem.BLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using SQLDLL;
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace NursingDocumentInfoSystem
{
    public partial class GestationEntryForm : XtraForm
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;   //员工信息
        string Id = string.Empty;
        PatientInfo Patientinfo;    //患者信息
        #endregion

        #region 窗体函数
        public GestationEntryForm(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = loginUser;
            this.Patientinfo = patientInfo;
            this.Id = id;
            //绑定数据
            BindInfo(Id);
        }
        public GestationEntryForm(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = string.Empty;
            //绑定数据
            BindInfo(this.Id);
        }
        public GestationEntryForm(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = id;
            //绑定数据
            BindInfo(this.Id);
        }
        #endregion

        #region 初始化控件数据数据
        private void InitializationControlsData(Control.ControlCollection Controls)
        {
            string sign = txtQianMing.Text;
            foreach (var control in Controls)
            {
                if (control.GetType().Name == "ComboBoxEdit")
                {
                    ((ComboBoxEdit)control).EditValue = "请选择";
                }
                else if (control.GetType().Name == "CheckEdit")
                {
                    ((CheckEdit)control).Checked = false;
                }
                else if (control.GetType().Name == "TextEdit")
                {
                    ((TextEdit)control).Text = "";
                }
                else if (control.GetType().Name == "DateEdit")
                {
                    ((DateEdit)control).DateTime = System.DateTime.Now;
                }
                else if (control.GetType().Name == "TimeEdit")
                {
                    ((TimeEdit)control).Text = System.DateTime.Now.ToString("HH:mm");
                }
                else if (control.GetType().Name == "RichEditControl")
                {
                    ((RichEditControl)control).Text = "";
                }
                else if (control.GetType().Name == "DateTimePicker")
                {
                    ((DateTimePicker)control).Text = "00:00";
                }
            }
            txtQianMing.Text = sign;
        }
        #endregion

        #region 提交
        private void btn_Submit_Click(object sender, EventArgs e)
        {
            //日期判断
            if (string.IsNullOrEmpty(txtRiQi.Text.Trim()))
                txtRiQi.DateTime = DateTime.Now;
            //时间判断
            if (!txtShiJian.Text.Contains(":"))
            {
                txtShiJian.Focus();
                MessageBox.Show("时间需HH:mm格式，或使用双击显示当前时间");
                return;
            }

            Pregnancy pc = new Pregnancy();
            pc.RiQi = txtRiQi.DateTime.ToString("yyyy-MM-dd");   //日期
            pc.ShiJian = DateTimeTran.TimeFillZero(txtShiJian.Text);   //时间
            pc.XueYa_Zuo = txtXueYaZ.Text;    //血压左
            pc.XueYa_You = txtXueYaY.Text;   //血压右
            pc.TaiDongJiShu = txtTaiDongJiShu.Text;  //胎动计数
            pc.TaiFangWei = comboTaiFangWei.Text;   //胎方位
            pc.TaiXianLu = comboTaiXianLou.Text;   //胎先露
            pc.TaiXinWeiZhi = comboTaiXinWei.Text;   //胎心位置
            pc.TaiXinCiShu = txtTaiXinCiShu.Text;   //胎心次数
            pc.GongSuoChiXu = txtGongSuoChiXu.Text;    //宫缩持续
            pc.GongSuoJianXie = txtGongSuoJianXie.Text;   //宫缩间歇
            pc.GongSuoQiangDu = comGongKouQiangDu.Text;   //宫缩强度
            pc.GongKouWeiZhi = txtGongKouWeiZhi.Text;   //宫口位置
            pc.GongKouZhiDi = txtGongKouZhiDi.Text;   //宫口质地
            pc.GongKouDaXiao = txtGongSuoDaXiao.Text;   //宫口大小
            pc.XianLuGaoDi = txtXianLouGaoDi.Text;   //先露高低
            pc.TaiMo = comboTaiMo.Text;   //胎膜
            pc.CuiChanSuNongDu = txtCuiChanSuDianDiNongDu.Text;   //催产素点滴_浓度
            pc.CuiChanSuDiShu = txtCuiChanSuDianDiDiShu.Text;    //催产素点滴_滴数
            pc.QiTa = memoQiTa.Text;   //其他
            if (string.IsNullOrEmpty(Id))
                pc.QianMing = LoginUser.UserName;
            else
                pc.QianMing = txtQianMing.Text;    //签名
            pc.FormId = (int)FormType.Fetation;     //表单ID
            pc.FormName = EnumHelper.GetDescription(FormType.Fetation);    //表单名字
            if (string.IsNullOrEmpty(this.Id))
            {
                pc.syncSN = DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(100, 10000).ToString();
                txtHTSN.Text = pc.syncSN;
            }
            else
            {
                pc.syncSN = txtHTSN.Text;
            }
            record.FormId = (int)FormType.Fetation;  //表单ID
            record.FormName = EnumHelper.GetDescription(FormType.Fetation);    //表单名字
            record.PatientId = Patientinfo.PATIENT_ID;      //住院号
            record.VisitId = Patientinfo.VISIT_ID;   //住院次数
            record.PatientName = Patientinfo.PATIENT_NAME;    //患者姓名
            record.BedNo = Patientinfo.BED_NO;   //床号
            record.DepartmentId = Patientinfo.DEPARTMENT_ID;   //当前科室ID
            record.DepartmentName = Patientinfo.DEPARTMENT_NAME;   //当前科室名字
            record.AreaId = Patientinfo.NURSE_CELL_CODE;    //当前患者病区ID
            record.AreaName = Patientinfo.NURSE_CELL_NAME;    //当前患者病区名称
            record.AssessmentDate = Convert.ToDateTime(pc.RiQi + " " + pc.ShiJian);//日期
            record.Id = string.IsNullOrEmpty(Id) ? 0 : Convert.ToInt32(Id);
            record.syncSN = txtHTSN.Text;
            record.NursingFormData = XmlUtil.ObjectToXML(pc, Encoding.UTF8);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;

                #region 上传海泰
                if (Base.ESSwitchHT.Equals("1"))
                {
                    StringBuilder data = new StringBuilder();
                    data.Append("<NursingRecordDocument>");

                    data.Append("<Systolicpressure>").Append(pc.XueYa_Zuo).Append("</Systolicpressure>");
                    data.Append("<Diastolicpressure>").Append(pc.XueYa_You).Append("</Diastolicpressure>");

                    data.Append("<specialbilleles>");//(一条记录对应多个项目，specialbillele节点循环增加)

                    data.Append("<specialbillele>");
                    data.Append("<option>1</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.973</eleid>");
                    data.Append("<elename>宫缩强度</elename>");//HT51.02.973	宫缩强度	下拉
                    data.Append("<elevalue>").Append(pc.GongSuoQiangDu).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.976</eleid>");
                    data.Append("<elename>宫口大小(cm)</elename>");//HT51.02.976	宫口大小	文本
                    data.Append("<elevalue>").Append(pc.GongKouDaXiao).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.979</eleid>");
                    data.Append("<elename>催产素点滴浓度(%)</elename>");//HT51.02.979	催产素点滴浓度	文本
                    data.Append("<elevalue>").Append(pc.CuiChanSuNongDu).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.980</eleid>");
                    data.Append("<elename>催产素点滴滴数(滴/分)</elename>");//HT51.02.980	催产素点滴滴数	文本
                    data.Append("<elevalue>").Append(pc.CuiChanSuDiShu).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.31.061</eleid>");
                    data.Append("<elename>其他</elename>");//HT51.02.31.061	其他	多行文本
                    data.Append("<elevalue>").Append(pc.QiTa).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.1008</eleid>");
                    data.Append("<elename>宫缩持续</elename>");//HT51.02.1008	宫缩持续	文本
                    data.Append("<elevalue>").Append(pc.GongSuoChiXu).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>PRESSURE</eleid>");
                    data.Append("<elename>血压</elename>");//PRESSURE	血压	文本
                    data.Append("<elevalue>").Append(pc.XueYa_Zuo).Append("/").Append(pc.XueYa_You).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>1</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.967</eleid>");
                    data.Append("<elename>胎方位</elename>");//HT51.02.967	胎方位	下拉
                    data.Append("<elevalue>").Append(pc.TaiFangWei).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>1</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.968</eleid>");
                    data.Append("<elename>胎先露</elename>");//HT51.02.968	胎先露	下拉
                    data.Append("<elevalue>").Append(pc.TaiXianLu).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>1</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.970</eleid>");
                    data.Append("<elename>胎心位置</elename>");//HT51.02.970	胎心位置	下拉
                    data.Append("<elevalue>").Append(pc.TaiXinWeiZhi).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.971</eleid>");
                    data.Append("<elename>胎心次数</elename>");//HT51.02.971	胎心次数	文本
                    data.Append("<elevalue>").Append(pc.TaiXinCiShu).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.1007</eleid>");
                    data.Append("<elename>宫缩间隔</elename>");//HT51.02.1007	宫缩间隔	文本
                    data.Append("<elevalue>").Append(pc.GongSuoJianXie).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.966</eleid>");
                    data.Append("<elename>胎动计数</elename>");//HT51.02.966	胎动计数	文本
                    data.Append("<elevalue>").Append(pc.TaiDongJiShu).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.975</eleid>");
                    data.Append("<elename>宫口质地</elename>");//HT51.02.975	宫口质地	文本
                    data.Append("<elevalue>").Append(pc.GongKouZhiDi).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.974</eleid>");
                    data.Append("<elename>宫口位置</elename>");//HT51.02.974	宫口位置	文本
                    data.Append("<elevalue>").Append(pc.GongKouWeiZhi).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>1</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.978</eleid>");
                    data.Append("<elename>胎膜</elename>");//HT51.02.978	胎膜	下拉
                    data.Append("<elevalue>").Append(pc.TaiMo).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("<specialbillele>");
                    data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                    data.Append("<eleid>HT51.02.977</eleid>");
                    data.Append("<elename>先露高低</elename>");//HT51.02.977	先露高低	文本
                    data.Append("<elevalue>").Append(pc.XianLuGaoDi).Append("</elevalue>");//元素值
                    data.Append("</specialbillele>");

                    data.Append("</specialbilleles> </NursingRecordDocument>");

                    SynchroBLL synchro = new SynchroBLL();
                    ClinicalNursinguser cn = new ClinicalNursinguser();
                    cn.PatientId = record.PatientId;
                    cn.PatientName = record.PatientName;
                    cn.DepId = record.DepartmentId;
                    cn.DepName = record.DepartmentName;
                    cn.AreaId = record.AreaId;
                    cn.AreaName = record.AreaName;
                    cn.data = data.ToString();
                    cn.Date = Convert.ToDateTime(record.AssessmentDate).ToString("yyyy-MM-dd");
                    cn.Time = Convert.ToDateTime(record.AssessmentDate).ToString("HH:mm");
                    cn.NursName = txtQianMing.Text;
                    cn.VisitId = record.VisitId;
                    cn.UpdateuserId = this.LoginUser.UserId;
                    cn.UpdateuserName = this.LoginUser.UserName;

                    cn.syncSN = txtHTSN.Text;
                    if (string.IsNullOrEmpty(this.Id))
                    {
                        synchro.clinicalNursingInfoAsyncToEMRDelegateMethod(cn, "003", "1");
                    }
                    else
                    {
                        synchro.clinicalNursingInfoAsyncToEMRDelegateMethod(cn, "003", "2");
                    }
                }
                #endregion
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion

        #region 取消
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region 清空
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Empty_Click(object sender, EventArgs e)
        {
            InitializationControlsData(groupControl1.Controls);
        }
        #endregion

        #region 显示赋值
        public void BindInfo(string Id)
        {
            #region 自动获取数据
            textHuanZheHao.Text = Patientinfo.PATIENT_ID;    //患者号
            textXingMing.Text = Patientinfo.PATIENT_NAME;   //姓名
            textChuangHao.Text = Patientinfo.BED_NO;   //床号
            txtQianMing.Text = LoginUser.UserName;   //护士签名
            #endregion

            txtRiQi.DateTime = DateTime.Now;    //默认显示当前日期
            txtShiJian.Text = DateTime.Now.ToString("HH:mm");
            if (string.IsNullOrEmpty(Id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            Pregnancy pcs = new Pregnancy();
            pcs = XmlUtil.XMLToObject<Pregnancy>(NursingFormData, Encoding.UTF8);
            if (!string.IsNullOrEmpty(pcs.RiQi))
                txtRiQi.DateTime = Convert.ToDateTime(pcs.RiQi);   //日期
            txtXueYaZ.Text = pcs.XueYa_Zuo;    //血压_左
            txtXueYaY.Text = pcs.XueYa_You;   //血压_右
            txtTaiDongJiShu.Text = pcs.TaiDongJiShu;    //胎动计数
            comboTaiFangWei.Text = pcs.TaiFangWei;    //胎方位
            comboTaiXianLou.Text = pcs.TaiXianLu;   //胎先露
            comboTaiXinWei.Text = pcs.TaiXinWeiZhi;    //胎心位置
            txtTaiXinCiShu.Text = pcs.TaiXinCiShu;   //胎心次数
            txtGongSuoChiXu.Text = pcs.GongSuoChiXu;    //宫缩持续
            txtGongSuoJianXie.Text = pcs.GongSuoJianXie;     //宫缩间歇
            comGongKouQiangDu.Text = pcs.GongSuoQiangDu;    //宫缩强度
            txtGongKouWeiZhi.Text = pcs.GongKouWeiZhi;    //宫口位置
            txtGongKouZhiDi.Text = pcs.GongKouZhiDi;    //宫口质地
            txtGongSuoDaXiao.Text = pcs.GongKouDaXiao;     //宫口大小
            txtXianLouGaoDi.Text = pcs.XianLuGaoDi;    //先露高低
            comboTaiMo.Text = pcs.TaiMo;    //胎膜
            txtCuiChanSuDianDiNongDu.Text = pcs.CuiChanSuNongDu;    //催产素点滴浓度
            txtCuiChanSuDianDiDiShu.Text = pcs.CuiChanSuDiShu;     //催产素点滴滴数
            memoQiTa.Text = pcs.QiTa;    //其他
            txtShiJian.Text = DateTimeHelper.HHmmssToHHmm(pcs.ShiJian);     //时间
            txtQianMing.Text = pcs.QianMing;//签名
            txtHTSN.Text = pcs.syncSN;
        }
        #endregion

        #region 设置 enter键当做Tab切换
        /// <summary>
        /// 设置 enter键当做Tab切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void TemperatureEdit1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
        #endregion

        #region 双击显示当前时间
        private void txtShiJian_DoubleClick(object sender, EventArgs e)
        {
            txtShiJian.Text = DateTime.Now.ToString("HH:mm");
        }
        #endregion
    }
}
